Namespace("sketch.ui");

(function(ui) {
    var fwk = sketch.framework;
    var TOP_HEIGHT = 50;
    ui.iphone.ActionSheet = klass(fwk.Container, (function() {
        return {
            __type__:"ui.iphone.ActionSheet",
            _callSuper:false,
            _constructor:function() {
                ui.iphone.ActionSheet.SuperConstructor.call(this, false);
                this._fillTop = fwk.Resources["actionsheet_top"];
                this._fillBody = fwk.Resources["actionsheet_body"];
                this._inarrange = false;
                this.height(60);
                this.width(320);

                var secondButton = new ui.iphone.BarItem();
                secondButton.style('Silver');
                secondButton.label('Second Button');
                this.add(secondButton);

                var cancelButton = new ui.iphone.BarItem();
                cancelButton.style('Black');
                cancelButton.label('Cancel');
                this.add(cancelButton);
                this.dropPositioning('vertical');
                this.enableGroupLocking(true);
            },
            drawSelf:function(context) {
                var w = this.width(),
                    h = this.height(),
                    x = this.left(),
                    y = this.top();

                var fillTop = this._fillTop.toBrush(context, 0, y, 0, y + TOP_HEIGHT);
                var fillBody = this._fillBody.toBrush(context, 0, y + TOP_HEIGHT, 0, y + h);
                context.save();
                context.fillStyle = fillTop;
                context.fillRect(x, y, w, TOP_HEIGHT);
                context.fillStyle = fillBody;
                context.fillRect(x, y + TOP_HEIGHT, w, h - TOP_HEIGHT);
                context.restore();
                ui.iphone.ActionSheet.Super.drawSelf.call(this, context);
            },
            arrange:function() {
                var length = this.children.count();
                if (length === 0) {
                    return;
                }
                var w = this.width(),
                    h = this.height(),
                    x = this.left(),
                    y = this.top();

                var requiredSize = 20;
                this.children.each(function(i, child) {
                    requiredSize += child.height();
                    if (i === 0) {
                        requiredSize += 20;
                    } else {
                        requiredSize += 10;
                    }
                });

                var pos = requiredSize - 20;
                this.children.eachReverse(function(i, child) {
                    child.left(20);
                    child.width(w - 40);
                    pos -= child.height();
                    child.top(pos);
                    if (i === length - 1) {
                        pos -= 20;
                    } else {
                        pos -= 10;
                    }
                });

                this.height(requiredSize);
            },
            add:function(element) {
                ui.iphone.ActionSheet.Super.add.call(this, element);
            },
            remove:function(element) {
                ui.iphone.ActionSheet.Super.remove.call(this, element);
            },
            canAccept:function(element) {
                return true;
            }
        };
    })());
})(sketch.ui);